package com.wfl.sampling.utils;

import cn.hutool.json.ObjectMapper;
import cn.hutool.jwt.JWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils {
    private static final Long expire = 36000*1000L;
    private static final String signKey = "woklio";

    //获取jwt令牌，返回jwt令牌字符串
    public static String generateJwt(Map<String,Object> claims){
        String jwt;
        jwt= Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256,signKey)//签名算法和数字秘钥
                .setExpiration(new Date(System.currentTimeMillis()+expire))
                .compact();//获得一个字符串返回值
        return jwt;
    }

    //校验jwt令牌，校验失败则抛出异常
    public static Claims parseJwt(String jwt){
        Claims claims;
        claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
    public static String refreshJwt(String jwt) {
        Claims claims = parseJwt(jwt);
        Map<String, Object> claimsMap = new HashMap<>(claims);
        return generateJwt(claimsMap);
    }

}
